package com.ruoyi.sl.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sl.domain.*;
import com.ruoyi.sl.pojo.SlHrworkQuery;
import com.ruoyi.sl.pojo.SlOddjobDateForm;
import com.ruoyi.sl.pojo.SlRworkDateForm;
import com.ruoyi.sl.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 零工报工Controller
 * 
 * @author admin
 * @date 2024-06-11
 */
@RestController
@RequestMapping("/sl/oddjob")
public class SlOddjobController extends BaseController
{
    @Autowired
    private ISlOddjobService slOddjobService;

    @Autowired
    private ISlWmService slWmService;
    @Autowired
    private ISlTeamService slTeamService;
    @Autowired
    private ISlModuleService slModuleService;
    @Autowired
    private ISlWorkshopService slWorkshopService;
    @Autowired
    private ISlGprocessService slGprocessService;
    @Autowired
    private ISlProcessService slProcessService;

    /**
     * 查询未提交的报工项列表
     */
    @GetMapping("/notlist")
    public TableDataInfo notlist(SlOddjob slOddjob)
    {
        //只查询登录用户自己
        LoginUser loginUser = getLoginUser();
        slOddjob.setOddjobUser(getLoginUser().getUsername());
        List<SlOddjob> list = slOddjobService.selectSlOddjobListByNoSubmit(slOddjob);
        return getDataTable(list);
        // return success(list);

    }


    /**
     * 根据报工单ID查询报工项
     */
    @GetMapping(value =  "/rfw/{rfworkId}")
    public TableDataInfo getListByrfworkId(@PathVariable("rfworkId") Long rfworkId)
    {
        List<SlOddjob> list = slOddjobService.selectSlOddjobListByRfworkId(rfworkId);
        return getDataTable(list);
    }



    /**
     * 查询零工报工列表
     */
    @PreAuthorize("@ss.hasPermi('sl:oddjob:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlOddjob slOddjob)
    {
        startPage();
        //只查询登录用户自己的当日的报工
        LoginUser loginUser = getLoginUser();

        slOddjob.setOddjobUser(getLoginUser().getUsername());
        slOddjob.setOddjobDate(DateUtils.dateTime(DateUtils.YYYY_MM_DD,DateUtils.getDate()));

        List<SlOddjob> list = slOddjobService.selectSlOddjobList(slOddjob);
        return getDataTable(list);
    }

    /**
     * 查询零工报工列表
     */
    @PreAuthorize("@ss.hasPermi('sl:oddjob:list')")
    @GetMapping("/hlist")
    public TableDataInfo hlist(SlHrworkQuery slHrworkQuery)
    {
        //startPage();
        //没有报工人则查询自己的
//        if(StringUtils.isNull(slOddjob.getOddjobUser())){
//            slOddjob.setOddjobUser(getLoginUser().getUsername());
//        }
       // List<SlOddjob> list = slOddjobService.selectSlOddjobListByRfworkId(slHrworkQuery.getRfworkId());
        List<SlOddjob> list;
        if(StringUtils.isNotNull(slHrworkQuery.getMemberUser())){
            list = slOddjobService.selectSlOddjobListByRfworkIdAndMember(slHrworkQuery.getRfworkId(),slHrworkQuery.getMemberUser());
        }else{
            list = slOddjobService.selectSlOddjobListByRfworkId(slHrworkQuery.getRfworkId());
        }
        return getDataTable(list);
    }


    /**
     * 导出零工报工列表
     */
    @PreAuthorize("@ss.hasPermi('sl:oddjob:export')")
    @Log(title = "零工报工", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlOddjob slOddjob)
    {
        //如果不是管理员则只查询登录用户自己的当日的报工
        LoginUser loginUser = getLoginUser();
        if (StringUtils.isNotNull(loginUser.getUser()) && !loginUser.getUser().isAdmin()){
            slOddjob.setOddjobUser(getLoginUser().getUsername());
            slOddjob.setOddjobDate(DateUtils.dateTime(DateUtils.YYYY_MM_DD,DateUtils.getDate()));
        }
        List<SlOddjob> list = slOddjobService.selectSlOddjobList(slOddjob);
        ExcelUtil<SlOddjob> util = new ExcelUtil<SlOddjob>(SlOddjob.class);
        util.exportExcel(response, list, "零工报工数据");
    }

    /**
     * 获取零工报工详细信息
     */
    @PreAuthorize("@ss.hasPermi('sl:oddjob:query')")
    @GetMapping(value = "/{oddjobId}")
    public AjaxResult getInfo(@PathVariable("oddjobId") Long oddjobId)
    {
        AjaxResult ajax = AjaxResult.success();

        ajax.put("modules",slModuleService.selectModuleAll());
        if (StringUtils.isNotNull(oddjobId))
        {
            SlOddjob slOddjo = slOddjobService.selectSlOddjobByOddjobId(oddjobId);
            ajax.put(AjaxResult.DATA_TAG, slOddjo);
            //获取模块对应的出工方式
            if(StringUtils.isNotNull(slOddjo.getModuleId())){
                ajax.put("wms", slWmService.selectSlWmListByModuleId(slOddjo.getModuleId()));
                //获取模块对应的车间
                ajax.put("workshops", slWorkshopService.selectSlWorkshopListByModuleId(slOddjo.getModuleId()));

            }
            //获取车间对应的大工序列表
            if(StringUtils.isNotNull(slOddjo.getWorkshopId())){
                ajax.put("gprocesss", slGprocessService.selectSlGprocessListByWorkshopId(slOddjo.getWorkshopId()));
            }
            //获取大工序对应的小工序列表
            if(StringUtils.isNotNull(slOddjo.getGprocessId())){
                ajax.put("processs", slProcessService.selectSlProcessListByGprocessId(slOddjo.getGprocessId()));
            }
        }
        return ajax;
        //return success(slOddjobService.selectSlOddjobByOddjobId(oddjobId));
    }

    /**
     * 打开新增报工单时获取页面中的选项内容
     */
    @GetMapping("/option")
    public AjaxResult oddjobOptions(SlOddjob slOddjob)
    {
        AjaxResult ajax = AjaxResult.success();

        //SlTeam slTeam = slTeamService.selectSlTeamByLeaderUser(getLoginUser().getUsername());
        ajax.put("modules",slModuleService.selectModuleAll());

        if (StringUtils.isNotNull(slOddjob.getModuleId()))
        {
            ajax.put("workshops",slWorkshopService.selectSlWorkshopListByModuleId(slOddjob.getModuleId()));

            //指定模块下的出工方式
            List<SlWm> wmList = slWmService.selectSlWmListByModuleId(slOddjob.getModuleId());
            ajax.put("wms",wmList);
        }
        return ajax;
    }

    /**
     * 新增零工报工
     */
    @PreAuthorize("@ss.hasPermi('sl:oddjob:add')")
    @Log(title = "零工报工", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SlOddjob slOddjob)
    {
        slOddjob.setCreateBy(getUsername());
        slOddjob.setOddjobUser(getUsername());
        slOddjob.setOddjobDate(DateUtils.dateTime(DateUtils.YYYY_MM_DD,DateUtils.getDate()));
        return toAjax(slOddjobService.insertSlOddjob(slOddjob));
    }


    /**
     * 整张报工单提交
     */
    @PreAuthorize("@ss.hasPermi('sl:oddjob:add')")
    @Log(title = "整张报工单提交", businessType = BusinessType.INSERT)
    @PostMapping("/daterws")
    public AjaxResult submitDateOddjob(@RequestBody SlOddjobDateForm slOddjobDateForm)
    {
        return toAjax(slOddjobService.insertDateOddjobs(slOddjobDateForm));
    }

    /**
     * 修改零工报工
     */
    @PreAuthorize("@ss.hasPermi('sl:oddjob:edit')")
    @Log(title = "零工报工", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SlOddjob slOddjob)
    {
        slOddjob.setUpdateBy(getUsername());
        return toAjax(slOddjobService.updateSlOddjob(slOddjob));
    }

    /**
     * 更新报工单开始结束时间
     */
    @PreAuthorize("@ss.hasPermi('sl:oddjob:edit')")
    @Log(title = "零工报工", businessType = BusinessType.UPDATE)
    @PutMapping("/upd")
    public AjaxResult updateDateTime(@RequestBody SlOddjob slOddjob)
    {
        slOddjob.setUpdateBy(getUsername());
        return toAjax(slOddjobService.updateSlOddjobDateTime(slOddjob));
    }

    /**
     * 删除零工报工
     */
    @PreAuthorize("@ss.hasPermi('sl:oddjob:remove')")
    @Log(title = "零工报工", businessType = BusinessType.DELETE)
	@DeleteMapping("/{oddjobIds}")
    public AjaxResult remove(@PathVariable Long[] oddjobIds)
    {
        return toAjax(slOddjobService.deleteSlOddjobByOddjobIds(oddjobIds));
    }
}
